# Contributor: Alex McGrath <amk@amk.ie>
# Maintainer: Alex McGrath <amk@amk.ie>
pkgname=gotosocial
pkgver=0.16.0
pkgrel=2
pkgdesc="An ActivityPub social network server"
url="https://github.com/superseriousbusiness/gotosocial"
arch="all"
license="AGPL-3.0-only"
install="$pkgname.pre-install"
pkgusers="gotosocial"
pkggroups="gotosocial"
makedepends="go yarn nodejs"
subpackages="$pkgname-openrc"
_pkgver="${pkgver/_rc/-rc}"
source="https://github.com/superseriousbusiness/gotosocial/releases/download/v$_pkgver/gotosocial-$_pkgver-source-code.tar.gz
	gotosocial.initd
	10-config.patch
	go-mod.patch
	"
builddir="$srcdir"

options="!check" # lots of flakey tests

case "$CARCH" in
	# flaky test
	s390x) options="!check";;
esac

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

prepare() {
	default_prepare

	# Because patches modified go.mod, The vendor needs
	# to be updated.
	go mod vendor
}

build() {
	yarn --cwd ./web/source install
	yarn --cwd ./web/source ts-patch install
	yarn --cwd ./web/source build
	go build -o gotosocial -ldflags="-X 'main.Version=$_pkgver'" ./cmd/gotosocial
}

check() {
	# skip tests in internal/media as they're failing
	local pkgs="$(go list ./... | grep -v 'internal/media')"
	GTS_DB_TYPE="sqlite" GTS_DB_ADDRESS=":memory:" \
		go test $pkgs
}

package() {
	install -Dm755 "$srcdir"/gotosocial.initd \
		"$pkgdir"/etc/init.d/gotosocial
	install -Dm755 "$builddir"/gotosocial \
		-t "$pkgdir"/usr/bin/

	install -d -o gotosocial -g gotosocial "$pkgdir"/etc/gotosocial
	install -Dm640 "$builddir"/example/config.yaml "$pkgdir"/etc/gotosocial/gotosocial.yml

	mkdir -p "$pkgdir"/usr/share/webapps/gotosocial
	cp -r "$builddir"/web/assets "$pkgdir"/usr/share/webapps/gotosocial
	cp -r "$builddir"/web/template "$pkgdir"/usr/share/webapps/gotosocial

	install -d -o gotosocial -g gotosocial "$pkgdir"/var/lib/gotosocial
}

sha512sums="
3e64a7046921dd80a85b30d7875fa8625b20b72c60aba46b18dcf247a3d8950fde15e11a9056274bb14543bc09a242cbf574f1ca1f924a3964f45aac8cb7ff11  gotosocial-0.16.0-source-code.tar.gz
b84bbd5e00d886d018311898a3e212c9d800c1688a5bd27f159e08a224a3c774513236f67288a7d77f22c4b55b75322972909b545d243f033812ab757ec2fd4b  gotosocial.initd
e1030586395c7bcca2bf348f764bb51de75e503a29c163e95f155b2192e213db61376f0b15e957813be917ba7f5d28abc8c4e944971ea6db64088424add97874  10-config.patch
872b4c8a1db742e0d91e0aa13767343ddebc4bd9b125f4b5578bf9df2b41050b2c38b2ad64f90d59551e9a4fa8eb0014b42b1dbaea4161a6b02fc75be027ec15  go-mod.patch
"
